<template>
  <el-dialog :visible.sync="visible" width="520px" @close="cancel">
    <div>
      <el-form :ref="formName" :model="form" :rules="rules" label-width="110px">
        <el-form-item prop="company_id" label="客户">
          <el-select v-model="form.company_id" placeholder="请选择" filterable clearable>
            <el-option v-for="item in companyList" :key="item.id" :value="item.id" :label="item.name" />
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <div class="dialog-footer-center">
      <span>
        <el-button type="primary" :loading="btnLoading" @click="submit">保存</el-button>
        <el-button @click="cancel">取消</el-button>
      </span>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'CoverContractDialog',
  data() {
    return {
      visible: false,
      id: '', // 合同id
      formName: 'coverForm',
      form: {
        company_id: '' // 客户id
      },
      rules: {
        company_id: [
          { required: true, message: '请选择客户', trigger: ['change', 'blur'] }
        ]
      },
      btnLoading: false,
      companyList: []
    }
  },
  created() {
    const vm = this
    vm.getCompanyList() // 获取客户列表
  },
  methods: {
    // 获取客户列表
    getCompanyList() {
      const vm = this
      const params = {
        page: 1,
        limit: 100000000,
        is_status: 1
      }
      vm.$http.get('/company', { params }).then(res => {
        vm.companyList = res.data.filter(e => e.type === 0)
      })
    },
    // 显示
    show(id) {
      const vm = this
      vm.id = id
      vm.visible = true
    },
    // 关闭
    cancel() {
      const vm = this
      vm.visible = false
      vm.id = ''
      vm.form.company_id = ''
    },
    // 提交
    submit() {
      const vm = this
      vm.$refs[vm.formName].validate(valid => {
        if (valid) {
          vm.btnLoading = true
          const params = {
            id: vm.id,
            company_id: vm.form.company_id
          }
          vm.$http.post('/purchase-contract', params).then(res => {
            vm.$message.success('转为采购合同成功')
            vm.cancel()
            vm.$emit('submit')
            vm.btnLoading = false
          }).catch(() => {
            vm.btnLoading = false
          })
        }
      })
    }
  }
}
</script>

<style lang="less" scoped></style>
